// 封装表单渲染
import { ref } from "vue";

export function useFormActions({fields = []} = {}) {
  const formVisible = ref(false);
  const currentFormData = ref(null);

  // 操作表单弹窗
  function openForm(row, type = 'default') {
    currentFormData.value = row
      ? {
          ...pickFields(row, fields),
          formType: type // 添加表单类型信息
        }
      : createEmptyForm(fields);
    formVisible.value = true;
  }

  // 工具函数：提取指定字段
  function pickFields(obj, fieldNames) {
    return fieldNames.reduce((acc, key) => {
      acc[key] = obj[key];
      return acc;
    }, {});
  }

  // 创建空表单数据
  function createEmptyForm(fieldNames) {
    return fieldNames.reduce((acc, key) => {
      acc[key] = ['order_num', 'hidden', 'keep_alive'].includes(key) ? 0 : '';
      return acc;
    }, {});
  }

  return {
    formVisible,
    currentFormData,
    openForm
  };
}